package com.haoshuang.zhouzhoubang.ui.page.mine.notice.publish

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityPublishNoticeBinding
import com.haoshuang.zhouzhoubang.ui.page.mine.notice.member.PublishNoticeChooseMemberActivity
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.startPageForResult
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.isBold
import com.mufeng.libs.utils.view.visible

/**
 *  发布公告
 */
class PublishNoticeActivity : BaseVMActivity<PublishNoticeViewModel, ActivityPublishNoticeBinding>() {

    companion object {
        fun launch(context: Context){
            context.startPage<PublishNoticeActivity>()
        }
    }

    // 发布类型 1: 选择师傅, 2: 全部
    private var publishType = 1
    // 选中师傅的ids
    private var ids = ""

    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "发布公告"
        binding.titleBar.leftImageView().click { finish() }

        binding.tvMember.isSelected = true
        binding.tvMember.isBold = true
        binding.tvAll.isBold = false
        binding.tvMember.click {
            publishType = 1
            binding.tvMember.isSelected = true
            binding.tvAll.isSelected = false
            binding.llChooseMember.visible()
            binding.tvMember.isBold = true
            binding.tvAll.isBold = false
        }

        binding.tvAll.click {
            publishType = 2
            binding.tvMember.isSelected = false
            binding.tvAll.isSelected = true
            binding.llChooseMember.gone()
            binding.tvAll.isBold = true
            binding.tvMember.isBold = false
        }

        binding.llChooseMember.clickWithTrigger {
            // 去选择师傅
            startPageForResult<PublishNoticeChooseMemberActivity>("ids" to ids){code, data ->
                if (code == RESULT_OK){
                    ids = data?.getString("ids") ?: ""
                    val names = data?.getString("names") ?: ""
                    if (ids.isEmpty()){
                        binding.tvChooseMember.text = ""
                    }else{
                        binding.tvChooseMember.text = "已选择${names.split(",").size}位"
                    }
                }
            }
        }

        binding.tvPublish.clickWithTrigger {
            if (publishType == 1){
                if (ids.isEmpty()){
                    toast("请选择师傅")
                    return@clickWithTrigger
                }
            }
            val content = binding.etContent.text?.toString() ?: ""
            if (content.isEmpty()){
                toast("请输入公告内容")
                return@clickWithTrigger
            }
            vm.publishNotice(ids, content)
        }
    }

    override fun initData() {
    }

    override fun startObserve() {
        vm.publishNoticeData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                finish()
            }

        }
    }

}